//
//  HLSongPlayManager.m
//  HLMusicBox
//
//  Created by hulinhu on 2018/1/4.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

#import "HLSongPlayManager.h"
#import "HLSongViewModel.h"

@interface HLSongPlayManager ()

@property (nonatomic, strong) HLSongViewModel *songViewModel;

@end

@implementation HLSongPlayManager

+ (id)shareInstance {
    static dispatch_once_t onceToken = 0;
    __strong static id _shareManager = nil;
    dispatch_once(&onceToken, ^{
        _shareManager = [[self alloc] init];
    });
    return _shareManager;
}
-(void)requestCurrentPlaySong:(NSString *)song_Id playSongIndex:(NSInteger)playIndex {
    // 请求歌曲信息
    [[self.songViewModel.songItemCommand execute:song_Id] subscribeNext:^(NSDictionary *result) {
        self.ItemModel =  [HLSongItemModel yy_modelWithDictionary:result];
        [self getRequestLRCSongUrl:self.ItemModel.songinfo.lrclink];
        self.playSongIndex = playIndex;
    }];
}

-(void *)getRequestLRCSongUrl:(NSString *)lrcUrl {
    
    if (lrcUrl.length == 0  && lrcUrl == nil) return nil;
    NSURL *url = [NSURL URLWithString:lrcUrl];
    
    @try {
        NSData *lrcData = [NSData dataWithContentsOfURL:url];
        NSString *lrcString = [[NSString alloc] initWithData:lrcData encoding:NSUTF8StringEncoding];
        if (![lrcString isEqualToString:@""] && lrcString != nil) {
            [self.mLRCDictinary removeAllObjects];
            [self.mTimeArray removeAllObjects];
            NSArray *lrcArray = [lrcString componentsSeparatedByString:@"\n"];
            for (NSString *line in lrcArray) {
                if ([line containsString:@"[0"] ||
                    [line containsString:@"[1"] ||
                    [line containsString:@"[2"] ||
                    [line containsString:@"[3"]) {
                    NSArray *lineArr = [line componentsSeparatedByString:@"]"];
                    NSString *str1 = [line substringWithRange:NSMakeRange(3, 1)];
                    NSString *str2 = [line substringWithRange:NSMakeRange(6, 1)];
                    if ([str1 isEqualToString:@":"] && [str2 isEqualToString:@"."]) {
                        NSString *lrcStr = lineArr[1];
                        NSString *timeStr = [lineArr[0] substringWithRange:NSMakeRange(1, 5)];
                        [self.mLRCDictinary setObject:lrcStr forKey:timeStr];
                        [self.mTimeArray addObject:timeStr];
                    }
                }else {
                    continue;
                }
            }
        } else {
            [self.mLRCDictinary removeAllObjects];
            [self.mTimeArray removeAllObjects];
            [self.mLRCDictinary setObject:@"没有找到该歌词" forKey:@"0:0"];
            [self.mTimeArray addObject:@"0:0"];
        }
    } @catch (NSException *exception) {
        NSLog(@"获取歌词出错!");
    }
}

#pragma mark - int转NSString
-(NSString *)intToString: (int)needTransformInteger {
    
    //实现00：00这种格式播放时间
    int wholeTime = needTransformInteger;
    int min  = wholeTime / 60;
    int sec = wholeTime % 60;
    NSString *str = [NSString stringWithFormat:@"%02d:%02d", min , sec];
    
    return str;
}

#pragma mark - NSString转int
-(int) stringToInt: (NSString *)timeString {
    
    NSArray *strTemp = [timeString componentsSeparatedByString:@":"];
    int time = [strTemp.firstObject intValue] * 60 + [strTemp.lastObject intValue];
    return time;
    
}

#pragma mark
#pragma mark - lazy load

-(HLSongItemModel *)ItemModel {
    if (!_ItemModel) {
        _ItemModel = [[HLSongItemModel alloc] init];
    }
    return _ItemModel;
}

-(HLSongViewModel *)songViewModel {
    if (!_songViewModel) {
        _songViewModel = [[HLSongViewModel alloc] init];
    }
    return _songViewModel;
}
-(NSMutableDictionary *)mLRCDictinary {
    if (!_mLRCDictinary) {
        _mLRCDictinary = [[NSMutableDictionary alloc] init];
    }
    return _mLRCDictinary;
}
-(NSMutableArray *)mTimeArray {
    if (!_mTimeArray) {
        _mTimeArray = [[NSMutableArray alloc] init];
    }
    return _mTimeArray;
}

@end
